#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Solutionxx {
public:
    int minPathSum(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size();
        vector<vector<int>> dp(m + 1, vector<int>(n + 1, INT_MAX));
        dp[0][1] = dp[1][0] = 0;
        for (int i = 1; i <= m; i++)
            for (int j = 1; j <= n; j++) {
                dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i - 1][j - 1];
            }
        return dp[m][n];
    }
};

class Solution {
public:
    static const int N = 210;
    int dp[N][N] = { 0 };
    int jewelleryValue(vector<vector<int>>& frame) {
        int m = frame.size(), n = frame[0].size();
        for (int i = 1; i <= m; i++)
            for (int j = 1; j <= n; j++)
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + frame[i - 1][j - 1];
        return dp[m][n];
    }
};

class Solutionx {
public:
    int minFallingPathSum(vector<vector<int>>& matrix) {
        int m = matrix.size(), n = matrix[0].size();
        vector<vector<int>> dp(m + 1, vector<int>(n + 2));

        for (int i = 1; i <= m; i++)
            dp[i][0] = dp[i][n + 1] = 0x3f3f3f;

        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                int mc = min(dp[i - 1][j], dp[i - 1][j - 1]);
                dp[i][j] = min(mc, dp[i - 1][j + 1]) + matrix[i - 1][j - 1];
            }
        }

        int ret = 0x3f3f3;
        for (int i = 1; i <= n; i++)
            ret = min(ret, dp[m][i]);
        return ret;
    }
};

int main() {
    Solution s;
    /*s.jewelleryValue()*/

    return 0;
}